from langchain.llms import OpenAI
from langchain.agents import load_tools,initialize_agent,AgentType,Tool
from langchain.chat_models import ChatOpenAI
from langchain.agents.react.base import DocstoreExplorer
from langchain.docstore import Wikipedia
from langchain.tools import tool


# llm = OpenAI(
#     api_key='hk-mtiquv1000041663a49a34520ad3294132cbce7abf1c2ef3',
#     base_url="https://api.openai-hk.com/v1"
# )
llm = ChatOpenAI(api_key='hk-mtiquv1000041663a49a34520ad3294132cbce7abf1c2ef3',
    base_url="https://api.openai-hk.com/v1")

@tool
def get_word_length(word):
    """计算单词的长度"""
    return len(word)



tools = [
    get_word_length
]
agent = initialize_agent(tools,llm,agent=AgentType.OPENAI_FUNCTIONS,verbose=True)
print(agent.run('秦朝建立是在什么时间？'))